package chapter04;

import java.util.*;

public class BST<E extends Comparable<? super E>> {
    public static void main(String[] args) {
        Random random = new Random();
        BST<Integer> bst = new BST<>();
        int[] arr = {28, 16, 13, 22, 30, 29, 42};
        for (int i = 0; i < arr.length; i++) {
            bst.add(arr[i]);
        }
        System.out.println("层序遍历");
        bst.levelOrder();
        System.out.println("contains方法");
        boolean contains = bst.contains(100);
        System.out.println(contains);
        System.out.println("前序遍历");
        bst.preOrder();
        System.out.println("后序遍历");
        bst.postOrder();
        System.out.println("前序遍历(非递归)");
        bst.preOrderNR();
        System.out.println("中序遍历");
        bst.inOrder();
        System.out.println("查找最小值");
        bst.removeMin();
        Integer min = bst.findMin();
        System.out.println(min);
        System.out.println("查找最大值");
        bst.removeMax();
        Integer max = bst.findMax();
        System.out.println(max);
    }


    private Node root;

    private int size;

    public BST() {
        this(null);
    }

    public BST(Node root) {
        this(root, 0);
    }

    public BST(Node root, int size) {
        this.root = root;
        this.size = size;
    }

    public void add(E e) {
        root = add(root, e);
    }

    /*public void add(Node node, E e) {
        if (node == null) {
            return;
        }
        if (e.equals(node.e)) {
            return;
        }
        else if (e.compareTo(node.e) < 0 && node.left == null) {
            node.left = new Node(e);
            size++;
            return;
        }
        else if (e.compareTo(node.e) > 0 && node.right == null) {
            node.right = new Node(e);
            size++;
            return;
        }

        if (e.compareTo(node.e) < 0) {
            add(node.left, e);
        }
        else{
            add(node.right, e);
        }
    }*/

    public Node add(Node node, E e) {
        if (node == null) {
            size++;
            return new Node(e);
        }
        if (e.compareTo(node.e) < 0) {
            node.left = add(node.left, e);
        }else if (e.compareTo(node.e) > 0) {
            node.right = add(node.right, e);
        }
        return node;
    }

    public boolean contains(E e) {
        return contains(root, e);
    }

    private boolean contains(Node node, E e) {
        if (node == null) {
            return false;
        }
        if (e.compareTo(node.e) == 0) {
            return true;
        } else if (e.compareTo(node.e) < 0) {
           return contains(node.left, e);
        } else {
            return contains(node.right, e);
        }
    }

    public void preOrder() {
        preOrder(root);
    }

    private void preOrder(Node node) {
        if (node == null) {
            return;
        }
        System.out.println(node.e);
        preOrder(node.left);
        preOrder(node.right);
    }

    public void preOrderNR() {
        Stack<Node> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            Node pop = stack.pop();
            System.out.println(pop.e);
            if (pop.right != null) {
                stack.push(pop.right);
            }
            if (pop.left != null) {
                stack.push(pop.left);
            }
        }
    }

    public void inOrder() {
        inOrder(root);
    }

    private void inOrder(Node node) {
        if (node == null) {
            return;
        }
        inOrder(node.left);
        System.out.println(node.e);
        inOrder(node.right);
    }

    public void postOrder() {
        postOrder(root);
    }

    private void postOrder(Node node) {
        if (node == null) {
            return;
        }
        postOrder(node.left);
        postOrder(node.right);
        System.out.println(node.e);
    }

    /**
     * 层序遍历
     */
    public void levelOrder() {
        Queue<Node> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            Node remove = queue.remove();
            System.out.println(remove.e);
            if (remove.left != null) {
                queue.add(remove.left);
            }
            if (remove.right != null) {
                queue.add(remove.right);
            }
        }
    }

    /**
     * 查詢最小值
     * @return
     */
    public E findMin() {
        if (size == 0)
            throw new IllegalArgumentException("BST is empty");
        return findMin(root).e;
    }

    /**
     * 查詢以node为根結點的最小值
     * @param node
     * @return
     */
    private Node findMin(Node node) {
        Node prev = node;
        while (prev.left != null) {
            prev = prev.left;
        }
        return prev;
    }

    /**
     * 查詢最大值
     * @return
     */
    public E findMax() {
        if (size == 0)
            throw new IllegalArgumentException("BST is empty");
        return findMax(root).e;
    }

    /**
     * 查詢以node為根節點的最大值
     * @param node
     * @return
     */
    private Node findMax(Node node) {
        if (node.right == null)
            return node;
        return findMax(node.right);
    }

    /**
     * 从二分搜索书中删除最小值所在节点
     * @return
     */
    public E removeMin() {
        E min = findMin();
        removeMin(root);
        return min;
    }

    /**
     * 删除掉以node为根的二分搜索树中的最小节点
     * @param node
     * @return 返回删除节点后新的二分搜索树的根
     */
    private Node removeMin(Node node) {
        if (node.left == null) {
            Node rightNode = node.right;
            node.right = null;
            size--;
            return rightNode;
        }
        node.left = removeMin(node.left);
        return node;
    }

    /**
     * 從二分搜索樹中刪除最小值所在節點
     * @return
     */
    private E removeMax() {
        E max = findMax();
        removeMax(root);
        return max;
    }

    /**
     * 從二分搜索樹中刪除以node為根節點的最小值
     * @param node
     * @return
     */
    private Node removeMax(Node node) {
        if (node.right == null) {
            Node leftNode = node.left;
            node.right = null;
            size--;
            return leftNode;
        }
        node.right = removeMax(node.right);
        return node;
    }

    public void remove(E e) {
        root = remove(root, e);
    }

    private Node remove(Node node, E e) {
        if (node == null) {
            return null;
        }
        if (e.compareTo(node.e) < 0) {
            node.left = remove(node.left, e);
            size--;
        } else if (e.compareTo(node.e) > 0) {
            node.right = remove(node.right, e);
            size--;
        } else {
            if (node.left == null) {

            }
        }
    }


    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        //generateBSTString(root, 0, res);
        return res.toString();
    }

    /**
     * 节点
     */
    public class Node {
        public E e;

        public Node left;

        public Node right;

        public Node(E e) {
            this(e, null, null);
        }

        public Node(E e, Node left, Node right) {
            this.e = e;
            this.left = left;
            this.right = right;
        }
    }
}
